home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / scsh-0.4 / scsh-0 / scsh-0.4.2 / scsh / scsh-tramp.c < prev    next >
C/C++ Source or Header  |  1995-10-13  |  2KB  |  74 lines

  1. /*
  2. ** Shell-script trampoline.
  3. ** Copyright (c) 1994 by Olin Shivers.
  4. /*
  5.  
  6. /* Unix #! shell scripts are not recursive. The interpreter you specify
  7. ** on the #! line cannot itself be a shell script. This is a problem for
  8. ** the Scheme shell, since it is implemented as a heap image executed
  9. ** by the Scheme 48 vm. This means that users cannot write shell scripts
  10. ** of the form:
  11. **     #!/usr/local/bin/scsh -s
  12. **     !#
  13. **     ...Scheme code goes here...
  14. **
  15. ** They must instead write:
  16. **     #!/usr/local/lib/scsh/scshvm \
  17. **     -o /usr/local/lib/scsh/scshvm -i /usr/local/lib/scsh/scsh.image -s
  18. **     ...Scheme code goes here...
  19. **
  20. ** This is gruesome and probably confusing to novices.
  21. **
  22. ** What we do is have this tiny little stub program play the role of scsh.
  23. ** It is compiled to a real Unix binary, but when it is executed, it simply
  24. ** execs the scsh virtual machine, passing it an argv composed of
  25. **     { "-o" "scshvm" "-i" "scsh.image"}
  26. ** prepended to whatever argv it was given. Now you can write shell scripts
  27. ** with
  28. **     #!/usr/local/bin/scsh -s
  29. ** triggers.
  30. **
  31. ** There are two downsides to doing things this way.
  32. ** 1. You pay an extra exec(2) at startup time. 
  33. **    And scsh starts up slow enough as it is.
  34. ** 2. You cannot specify extra arguments for the vm this way. The most
  35. **    important one you might want to specify is the heap size arg, -h.
  36. */
  37.  
  38. #include <errno.h>
  39. #include <unistd.h>
  40.  
  41. #ifndef VM
  42. #define VM "/usr/local/lib/scsh/scshvm"
  43. #endif
  44. #ifndef IMAGE
  45. #define IMAGE "/usr/local/lib/scsh/scsh.image"
  46. #endif
  47.  
  48. main(int argc, char *argv[])
  49. {
  50.     char **ap, **aq, **newav;
  51.  
  52.     /* Insert "-o" VM "-i" IMAGE between argv[0] and argv[1]. */
  53.  
  54.     argc += 4;                    /* We're adding 4 new elts. */
  55.     newav = (char **) malloc((argc+1) * sizeof(char*));    /* Alloc new argv. */
  56.     if( !newav ) {
  57.     perror(argv[0]);
  58.     exit(1);
  59.     }
  60.  
  61.     newav[0] = argv[0];        /* Install new header args. */
  62.     newav[1] = "-o";
  63.     newav[2] = VM;
  64.     newav[3] = "-i";
  65.     newav[4] = IMAGE;
  66.     
  67.     for(ap=&argv[0], aq=&newav[4]; *ap;)    /* Copy over orignal argv */
  68.     *++aq = *++ap;                /*   & the terminating NULL. */
  69.  
  70.     execv(VM, newav);                /* Do it. */
  71.     perror(argv[0]);
  72.     exit(-1);
  73.     }
  74.